package com.dhnsoft.education.controller.edu;


import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.dhnsoft.education.entity.edu.EduCourse;
import com.dhnsoft.education.entity.edu.EduCourseCollect;
import com.dhnsoft.education.service.edu.EduCourseCollectService;
import com.dhnsoft.education.service.edu.EduCourseService;
import com.dhnsoft.education.utils.JwtUtils;
import com.dhnsoft.education.utils.R;
import org.checkerframework.checker.units.qual.A;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.HttpServletRequest;
import java.util.ArrayList;
import java.util.List;

/**
 * <p>
 * 课程收藏 前端控制器
 * </p>
 *
 * @author 代浩楠
 * @since 2022-02-19
 */
@RestController
@RequestMapping("/eduservice/courseCollect")
public class EduCourseCollectController {

    @Autowired
    private EduCourseCollectService courseCollectService;

    @Autowired
    private EduCourseService courseService;

    //添加收藏课程
    @GetMapping("addCourseCollect/{courseId}")
    public R addCourseCollect(@PathVariable String courseId, HttpServletRequest request){
        String memberId = JwtUtils.getMemberIdByJwtToken(request);
        if(StringUtils.isEmpty(memberId)){
            System.out.println("未登录!!!");
            return R.error().code(28004).message("请登录");
        }
        EduCourseCollect courseCollect = new EduCourseCollect();
        courseCollect.setCourseId(courseId);
        courseCollect.setMemberId(memberId);
        courseCollectService.save(courseCollect);
        QueryWrapper<EduCourseCollect> wrapper = new QueryWrapper<>();
        wrapper.eq("course_id",courseId);
        wrapper.eq("member_id",memberId);
        EduCourseCollect eduCourseCollect = courseCollectService.getOne(wrapper);
        return R.ok().data("collectId",eduCourseCollect.getId());
    }
    //查询课程用户是否收藏
    @GetMapping("getCourseCollect/{courseId}")
    public R getCourseCollect(@PathVariable String courseId, HttpServletRequest request){
        String memberId = JwtUtils.getMemberIdByJwtToken(request);
        if(StringUtils.isEmpty(memberId)){
            System.out.println("未登录!!!");
            return R.error().code(28004).message("请登录");
        }
        QueryWrapper<EduCourseCollect> wrapper = new QueryWrapper<>();
        wrapper.eq("course_id",courseId);
        wrapper.eq("member_id",memberId);
        EduCourseCollect courseCollect = courseCollectService.getOne(wrapper);
        if(courseCollect!=null){
            return R.ok().data("collectId",courseCollect.getId());
        }else {
            return R.ok().data("collectId","-1");
        }

    }

    //查询一个用户的所有收藏
    @GetMapping("getCourseCollectByMemberId")
    public R getCourseCollect(HttpServletRequest request){
        String memberId = JwtUtils.getMemberIdByJwtToken(request);
        if(StringUtils.isEmpty(memberId)){
            System.out.println("未登录!!!");
            return R.error().code(28004).message("请登录");
        }
        QueryWrapper<EduCourseCollect> wrapper = new QueryWrapper<>();
        wrapper.eq("member_id",memberId);
        //根据用户id查询所有的收藏集合
        List<EduCourseCollect> list = courseCollectService.list(wrapper);
        //创建一个空的课程集合
        List<EduCourse> courseList = new ArrayList<>();
        for (EduCourseCollect courseCollect : list) {
            //根据收藏集合中的课程id查询课程
            QueryWrapper<EduCourse> wrapper2 = new QueryWrapper<>();
            wrapper2.eq("id",courseCollect.getCourseId());
            EduCourse one = courseService.getOne(wrapper2);
            courseList.add(one);
        }
        return R.ok().data("collectList",courseList);
    }

    //删除收藏课程
    @GetMapping("delCourseCollect/{id}")
    public R delCourseCollect(@PathVariable String id){
        QueryWrapper<EduCourseCollect> wrapper = new QueryWrapper<>();
        wrapper.eq("id",id);
        courseCollectService.remove(wrapper);
        return R.ok();
    }
}

